package com.villaday.idf.custom;


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.villaday.idf.R;


/**
 * 确认 对话框
 */
public class ConfirmReturnDialog extends DialogFragment implements View.OnClickListener {

    private static ConfirmReturnDialog confirmDialog;
    private Button btnCancel;
    private Button btnConfirm;
    private OnBtnClickListener onBtnClickListener;


    public static ConfirmReturnDialog newInstance(String title, String content, String cancel, String confirm) {
        confirmDialog = new ConfirmReturnDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("content", content);
        bundle.putString("cancel", cancel);
        bundle.putString("confirm", confirm);
        confirmDialog.setArguments(bundle);
        return confirmDialog;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = null;
        try {
            mView = getActivity().getLayoutInflater().inflate(R.layout.dialog_confirm_return, container);
            btnCancel = mView.findViewById(R.id.btn_cancelConfirm);
            btnConfirm = mView.findViewById(R.id.btn_deleteConfirm);
            TextView tvTitle = mView.findViewById(R.id.tv_confirm_title);
            TextView tvContent = mView.findViewById(R.id.tv_confirm_content);


            //接收信息
            tvContent.setText(getArguments().getString("content"));
            tvTitle.setText(getArguments().getString("title"));
            btnCancel.setText(getArguments().getString("cancel"));
            btnConfirm.setText(getArguments().getString("confirm"));

            btnCancel.setOnClickListener(this);
            btnConfirm.setOnClickListener(this);

            //点击对话框外不可取消
            getDialog().setCanceledOnTouchOutside(false);
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            return mView;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mView;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_deleteConfirm) {//确认
            try {
                onBtnClickListener.onBtnClick(v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (v.getId() == R.id.btn_cancelConfirm) {//取消
            try {
                onBtnClickListener.onDeselect(v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        dismiss();
        confirmDialog.dismissAllowingStateLoss();
    }


    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onBtnClick(View view);

        void onDeselect(View view);
    }

    @Override
    public void onDestroy() {
        confirmDialog = null;
        super.onDestroy();
    }
}
